package com.example.zhunongzonghe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zhunongzonghe.beans.ZnFarmhouseBean;
import com.example.zhunongzonghe.dao.ZnFarmhouseMapper;
import com.example.zhunongzonghe.entity.ZnFarmhouse;
import com.example.zhunongzonghe.entity.ZnUsers;
import com.example.zhunongzonghe.service.IZnFarmhouseService;
import com.example.zhunongzonghe.service.IZnUsersService;
import com.example.zhunongzonghe.service.ZnCollectHouseService;
import com.example.zhunongzonghe.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class ZnFarmhouseServiceImpl extends ServiceImpl<ZnFarmhouseMapper, ZnFarmhouse> implements IZnFarmhouseService {

    @Autowired
    ZnFarmhouseMapper znFarmhouseMapper;
    @Resource
    private ZnCollectHouseService collectHouseService;
    @Resource
    private IZnUsersService znUsersService;
    /**
     * 后台：查询全部，搜索
     * @param pageId
     * @param pageNum
     * @param searchInfo
     * @return
     */
    @Override
    public Page<ZnFarmhouse> SearcHouseAll(Integer pageId, Integer pageNum, Object searchInfo) {
        Page<ZnFarmhouse> page = new Page<>(pageId,pageNum);
        LambdaQueryWrapper<ZnFarmhouse> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(ZnFarmhouse::getHouseName,searchInfo)
                .or().like(ZnFarmhouse::getHousePrice,searchInfo)
                .or().like(ZnFarmhouse::getDistrictAddress,searchInfo);
        Page<ZnFarmhouse> znFarmhousePage = znFarmhouseMapper.selectPage(page,queryWrapper);
        return znFarmhousePage;
    }

    /**
     * 首页，农家乐信息
     * @return
     */
    @Override
    public List<ZnFarmhouse> SearcHomeHouseAll() {
        List<ZnFarmhouse> znFarmhouseList = znFarmhouseMapper.selectList(null);
        return znFarmhouseList;
    }


    /**
     * 单个农家乐信息
     * @param houseId
     * @return
     */
    @Override
    public ZnFarmhouse SearcHouseInfo(Integer houseId) {
        LambdaQueryWrapper<ZnFarmhouse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ZnFarmhouse::getHouseId,houseId);
        ZnFarmhouse znFarmhouse = znFarmhouseMapper.selectOne(wrapper);
        return znFarmhouse;
    }

    @Override
    public ZnFarmhouseBean SearcHouseDetails(Integer houseId) {
        LambdaQueryWrapper<ZnFarmhouse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ZnFarmhouse::getHouseId,houseId);
        ZnFarmhouse znFarmhouse = znFarmhouseMapper.selectOne(wrapper);
        ZnFarmhouseBean bean = BeanCopyUtils.copyBean(znFarmhouse,ZnFarmhouseBean.class);
        return bean;
    }

    @Override
    public ZnFarmhouseBean HouseDetails(Integer houseId, String username) {
        ZnUsers znUsers = znUsersService.getUserName(username);
        LambdaQueryWrapper<ZnFarmhouse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ZnFarmhouse::getHouseId,houseId);
        ZnFarmhouse znFarmhouse = znFarmhouseMapper.selectOne(wrapper);
        ZnFarmhouseBean bean = BeanCopyUtils.copyBean(znFarmhouse,ZnFarmhouseBean.class);
        bean.setCollectOrNot(collectHouseService.isCollect(houseId,znUsers.getId()));
        bean.setCollectCount(collectHouseService.CollectCounts(houseId));
        return bean;
    }

    @Override
    public ZnFarmhouseBean NotLogHouseDetails(Integer houseId) {
        LambdaQueryWrapper<ZnFarmhouse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ZnFarmhouse::getHouseId,houseId);
        ZnFarmhouse znFarmhouse = znFarmhouseMapper.selectOne(wrapper);
        ZnFarmhouseBean bean = BeanCopyUtils.copyBean(znFarmhouse,ZnFarmhouseBean.class);
        bean.setCollectCount(collectHouseService.CollectCounts(houseId));
        return bean;
    }

    /**
     * 农家乐页面分页显示
     * @param pageId
     * @param pageNum
     * @return
     */
    @Override
    public Page<ZnFarmhouse> ZnFarmhousePage(Integer pageId, Integer pageNum) {
        Page<ZnFarmhouse> page = new Page<>(pageId,pageNum);
        Page<ZnFarmhouse> znFarmhousePage = znFarmhouseMapper.selectPage(page,null);
        return znFarmhousePage;
    }
}
